package com.vital.mshopping;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.*;
import com.vital.dao.Bookmark;
import com.vital.dao.History;
import com.vital.dao.Location;
import com.vital.service.LocationService;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;

public class MapViewActivity extends MapActivity{
	//Location location;
	MapView mapView;
	LocationManager lm;
	LocationListener ll;
	double latGPS;
	double lngGPS;
	boolean addMyLocation = false;
	ArrayList<Location> mapview_serializable;
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mapview);
		//---Get widget---
		mapView = (MapView)findViewById(R.id.mapView);
		mapView.setBuiltInZoomControls(true);
		
		//---Get GPS location---
		LocationService locationService = new LocationService(this);
		latGPS = locationService.getLat();
		lngGPS = locationService.getLng();
				
		//---Retrieve map location from Detail Location Activity---
		Intent intent = this.getIntent();
		Bundle b = intent.getExtras();
		
		//---Get map_location from LocationActivity---
		if (b.getSerializable("map_location") != null) {
			Location location = new Location();
			location = (Location)b.getSerializable("map_location");
			addLocationToMap(location);
			new AddLocationToMap().execute(location);
		}
		
		//---Get mapview_serializable from LocationActivity---
		if (b.getSerializable("mapview_serializable") != null) {
			ArrayList<Location> locations = new ArrayList<Location>();
			locations = (ArrayList<Location>)b.getSerializable("mapview_serializable");
			for (int i = 0; i < locations.size(); i++) {
				new AddLocationToMap().execute(locations.get(i));
			}
			
		}
		
		//---Get objBookmark from BookmarkActivity---
		if (b.getSerializable("ObjBookmark") != null) {
			Bookmark bookmark = new Bookmark();
			bookmark = (Bookmark)b.getSerializable("ObjBookmark");
			Location location = new Location();
			location.setId(bookmark.getLocationId());
			location.setAddress(bookmark.getAddress());
			location.setAccuracy(bookmark.getAccuracy());
			location.setCategory(bookmark.getCategory());
			location.setLat(bookmark.getLat());
			location.setLng(bookmark.getLng());
			location.setPhone(bookmark.getPhone());
			location.setName(bookmark.getName());
			
			//---Add this location to map---
			new AddLocationToMap().execute(location);
		}
		
		//---Get objHistory from HistoryActivity---
		if (b.getSerializable("ObjHistory") != null) {
			History history = new History();
			history = (History)b.getSerializable("ObjHistory");
			
			Location location = new Location();
			location.setId(history.getLocationId());
			location.setAddress(history.getAddress());
			location.setAccuracy(history.getAccuracy());
			location.setCategory(history.getCategory());
			location.setLat(history.getLat());
			location.setLng(history.getLng());
			location.setPhone(history.getPhone());
			location.setName(history.getName());
			
			//---Add this location to map---
			new AddLocationToMap().execute(location);
		}
	}

	private void addLocationToMap(Location location) {
		List<Overlay> mapOverlays = mapView.getOverlays();
		if (!addMyLocation) {
			addMyLocation = addMyLocationToMap(mapOverlays, latGPS, lngGPS);
		}
		Drawable drawable = this.getResources().getDrawable(R.drawable.mapmarker_red);
		CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this, location);
		
		GeoPoint point = new GeoPoint((int)(location.getLat() * 1E6), (int)(location.getLng() * 1E6));
		OverlayItem overlayItem = new OverlayItem(point, location.getName(), location.getAddress());
		
		
		itemizedOverlay.addOverlay(overlayItem);
		mapOverlays.add(itemizedOverlay);
		
		MapController mapController = mapView.getController();
		mapController.animateTo(point);
		mapController.setZoom(17);
	}
	
	private boolean addMyLocationToMap(List<Overlay> overlays, double lat, double lng) {
		Drawable drawable = this.getResources().getDrawable(R.drawable.mymarker_blue);
		CustomItemizedOverlay itemizedOverlay = new CustomItemizedOverlay(drawable, this, null);
		
		GeoPoint point = new GeoPoint((int)(lat * 1E6), (int)(lng * 1E6));
		OverlayItem overlayItem = new OverlayItem(point, "My location", "");
		itemizedOverlay.addOverlay(overlayItem);
		overlays.add(itemizedOverlay);
		return true;
	}
	
	private class AddLocationToMap extends AsyncTask<Location, String, String> {

		private final ProgressDialog progress = new ProgressDialog(MapViewActivity.this);
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			progress.setMessage("Loading...");
			progress.show();
		}
		
		@Override
		protected String doInBackground(Location... params) {
			// TODO Auto-generated method stub
			for (int i = 0; i < params.length; i++) {
				addLocationToMap(params[i]);
			}
			return null;
		}

		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			//---Set adapter for list view category---
			if (progress.isShowing()) {
				progress.dismiss();
			}
			 
		}
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_map, menu);
		return true;
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

}
